package com.cndw.kungfu.model.goods.domain;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.XmlProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.goodsvo.GoodsVO;

public class ExchangeShop {
	
	private int type ;
	private int id;
	private int goodsId;
	private String name;
	private int num;
	private int cost;
	private int level;
	private GoodsVO goodsVO;
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getGoodsId() {
		return goodsId;
	}
	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getCost() {
		return cost;
	}
	public void setCost(int cost) {
		this.cost = cost;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public GoodsVO getGoodsVO() {
		return goodsVO;
	}
	public void setGoodsVO(GoodsVO goodsVO) {
		this.goodsVO = goodsVO;
	}
	
	private static final Map<Integer, Map<Integer, ExchangeShop>> dataMap = new HashMap<Integer, Map<Integer, ExchangeShop>>();

	private static final String configFile = "exchangeShop.xml";

	static {
		load();
	}

	public static synchronized void load() {
		if (dataMap.isEmpty()) {
			String fileName = ConstantsConfig.getLocaleResource() + configFile;
			List<ExchangeShop> dataList = XmlProcesser.processXml(ExchangeShop.class, fileName);
			if (dataList != null && dataList.size() > 0) {
				for (ExchangeShop e : dataList) {
					GoodsMode goodsMode = Context.getGoodsServer().getModeById(e.getGoodsId());
					if (goodsMode == null) {
						LogUtil.getMain().error("exchangeShop goods not exist goodsId:" + e.getGoodsId());
						continue;
					}
					e.setGoodsVO(goodsMode.toGoodsVO());
					if (!dataMap.containsKey(e.getType())) {
						dataMap.put(e.getType(), new HashMap<Integer, ExchangeShop>());
					} 
					dataMap.get(e.getType()).put(e.getId(), e);
				}
			}
		}
	}

	public static synchronized void clear() {
		dataMap.clear();
		load();
	}

	public static Collection<ExchangeShop> getAll(int type) {
		if (!dataMap.containsKey(type)) {
			return null;
		}
		return dataMap.get(type).values();
	}

	public static ExchangeShop getOne(int type, int id) {
		if (!dataMap.containsKey(type)) {
			return null;
		}
		return dataMap.get(type).get(id);
	}
}
